package com.tbit.utils;

import com.tbit.uqbike.object.pojo.RideLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.List;

/**
 * @Author HZW
 * @ClassName Easy
 * @Description
 * @date 2022/4/20 11:08
 * @Version 1.0
 */
@Slf4j
public class Easy {
    /**
     * 向Excel中写入数据
     *
     * @param
     * @throws Exception
     */
    public static void excelData(List<RideLog> data, String filePath) throws IOException {
        boolean flag = fileExist(filePath);
        if (flag) {
            addExcel(data);
        } else {
            writeExcel(data, filePath);
        }
    }

    //判断文件是否存在
    public static boolean fileExist(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        flag = file.exists();
        return flag;
    }

    //向Excel中写数据
    public static void writeExcel(List<RideLog> list, String filePath) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("student");
        //第一行表头
        XSSFRow firstRow = sheet.createRow(0);
        XSSFCell cells[] = new XSSFCell[3];

        String[] titles = new String[]{"订单编号", "订单金额", "手机号", "车辆编号", "设备编号", "开始时间", "结束时间", "里程(公里)", "支付时间"};
        //循环设置表头信息
        for (int i = 0; i < titles.length; i++) {
            cells[0] = firstRow.createCell(i);
            cells[0].setCellValue(titles[i]);
        }

        //遍历list,将数据写入Excel中
        for (int i = 0; i < list.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            RideLog rideLog = list.get(i);
            //第一列
            XSSFCell cell = row.createCell(0);
            cell.setCellValue(rideLog.getOrderNO());
            //第二列
            cell = row.createCell(1);
            cell.setCellValue(rideLog.getMoney());
            //第三列
            cell = row.createCell(2);
            cell.setCellValue(rideLog.getPhone());
            //第四列
            cell = row.createCell(3);
            cell.setCellValue(rideLog.getMachineNO());
            //第五列
            cell = row.createCell(4);
            cell.setCellValue(rideLog.getUserCode());
            //第六列
            cell = row.createCell(5);
            cell.setCellValue(rideLog.getStartTime());
            //第七列
            cell = row.createCell(6);
            cell.setCellValue(rideLog.getEndTime());
            //第八列
            cell = row.createCell(7);
            cell.setCellValue(rideLog.getMileage());
            //第九列
            cell = row.createCell(8);
            cell.setCellValue(rideLog.getEndTime());
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(filePath);
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 向Excel中追加内容
     *
     * @param
     * @throws Exception
     */
    public static void addExcel(List<RideLog> data) throws IOException {
        FileInputStream in = new FileInputStream("D:\\Excel.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(in);
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFRow row = sheet.getRow(1);
        FileOutputStream out = new FileOutputStream("D:\\Excel.xlsx");
        for (int i = 0; i < data.size(); i++) {
            RideLog rideLog = data.get(i);
            //追加行数据,在现有行号后追加数据
            row = sheet.createRow((short) (sheet.getLastRowNum() + 1));
            //设置第一个（从0开始）单元格的数据
            row.createCell(0).setCellValue(rideLog.getOrderNO());
            //设置第二个（从0开始）单元格的数据
            row.createCell(1).setCellValue(rideLog.getMoney());
            row.createCell(2).setCellValue(rideLog.getPhone());
            row.createCell(3).setCellValue(rideLog.getMachineNO());
            row.createCell(4).setCellValue(rideLog.getUserCode());
            row.createCell(5).setCellValue(rideLog.getStartTime());
            row.createCell(6).setCellValue(rideLog.getEndTime());
            row.createCell(7).setCellValue(rideLog.getMileage());
            row.createCell(8).setCellValue(rideLog.getPayTime());
        }
        try {
            out.flush();
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
